import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';

import {
  PageVo, ApplicationDetail, ProjectCount,
  ProjectOverview, AddVo, EchartsVo, UnauthorizedListVo, HighRiskListVo, PorjectLicenseListsVo, ProjectJarListVo,
  PorjectCVEListVo, CVEGrowVo, CVEScoreVo
} from 'src/app/shared/models';
@Injectable({
  providedIn: 'root'
})
export class BulletinService {
  private _appInfo: BehaviorSubject<ApplicationDetail> = new BehaviorSubject(null);
  get appInfo$() { return this._appInfo.asObservable(); }
  set appInfo(appInfo: ApplicationDetail) { this._appInfo.next(appInfo); }

  constructor(private apiService: ApiService) { }


  /**
   * 项目头部的一些数据
   */
  projectCount(projectUuid?: string) {
    if (projectUuid) {
      const params = new HttpParams()
        .append('projectUuid', `${projectUuid}`);
      return this.apiService.get('/project/data/count', { params })
        .pipe(
          map((jsonResult: JsonResult<ProjectCount>) => jsonResult)
        );
    } else {
      return this.apiService.get('/project/data/count')
        .pipe(
          map((jsonResult: JsonResult<ProjectCount>) => jsonResult)
        );
    }
  }

  /**
   * 图表：项目概况
   */
  getProjectOverview(): Observable<PageVo<ProjectOverview>> {
    return this.apiService.get('/project/count')
      .pipe(
        map((jsonResult: JsonResult<PageVo<ProjectOverview>>) => jsonResult.data)
      );
  }


  /**
   * groupadmin下，增加新项目
   * @param info 项目信息
   */
  addProject(info: any) {
    return this.apiService.post('/project/add', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * 图表：CVE的分值
   */
  getCveCount(projectUuid?: string) {
    if (projectUuid) {
      const params = new HttpParams()
        .append('projectUuid', `${projectUuid}`);
      return this.apiService.get('/cve/count', { params })
        .pipe(
          map((jsonResult: JsonResult<EchartsVo>) => jsonResult)
        );
    } else {
      return this.apiService.get('/cve/count')
        .pipe(
          map((jsonResult: JsonResult<EchartsVo>) => jsonResult)
        );
    }
  }
 /**
   * 图表：CVE的分值(new)
   */
  getCveScore() {
      return this.apiService.get('/cve/count').pipe(
          map((jsonResult: JsonResult<CVEScoreVo>) => jsonResult)
        );
  }
  /**
   * 图表-列表：许可使用情况
   */
  getPermission(projectUuid?: string) {
    if (projectUuid) {
      const params = new HttpParams()
        .append('projectUuid', `${projectUuid}`);
      return this.apiService.get('/license/count', { params })
        .pipe(
          map((jsonResult: JsonResult<EchartsVo>) => jsonResult)
        );
    } else {
      return this.apiService.get('/license/count')
        .pipe(
          map((jsonResult: JsonResult<EchartsVo>) => jsonResult)
        );
    }

  }

  /**
   * 图表：高危Top 10
   * @param projectUuid 项目唯一标识
   */
  getHighRisk(projectUuid?: string) {
    if (projectUuid) {
      const params = new HttpParams()
        .append('projectUuid', `${projectUuid}`);
      return this.apiService.get('/highrisk/jar/ten', { params })
        .pipe(
          map((jsonResult: JsonResult<HighRiskListVo>) => jsonResult)
        );
    } else {
      return this.apiService.get('/highrisk/jar/ten')
        .pipe(
          map((jsonResult: JsonResult<HighRiskListVo>) => jsonResult)
        );
    }
  }

  /**
   * 图表-列表：未经授权的软件
   */
  getUnauthorizedPage(projectUuid?: string) {
    if (projectUuid) {
      const params = new HttpParams()
        .append('projectUuid', `${projectUuid}`);
      return this.apiService.get('/unauthorized/jar/ten', { params })
        .pipe(
          map((jsonResult: JsonResult<UnauthorizedListVo>) => jsonResult)
        );
    } else {
      return this.apiService.get('/unauthorized/jar/ten')
        .pipe(
          map((jsonResult: JsonResult<UnauthorizedListVo>) => jsonResult)
        );
    }
  }

  /**
   * 项目详情页-组件等级（项目首页没有此图表）
   * @param projectUuid 项目Uuid
   */
  getJarGrade(projectUuid: string) {
    const params = new HttpParams()
      .append('projectUuid', `${projectUuid}`);
    return this.apiService.get('/jar/count', { params })
      .pipe(
        map((jsonResult: JsonResult<UnauthorizedListVo>) => jsonResult)
      );
  }

  /**************************** 页面头部数据，点击进入相应的列表 ********************************/
  /**
   * 获取组件列表
   * @param pageIndex 页码
   * @param pageSize 每页几条信息
   */
  getJarPage(pageIndex: number, pageSize: number, projectUuid?: string, filter?: string): Observable<PageVo<ProjectJarListVo>> {
    if (filter && filter.length > 1 && filter.length < 50) {
      if (projectUuid) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', `${filter}`)
          .append('projectUuid', `${projectUuid}`);
        return this.apiService.get('/project/jar/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<ProjectJarListVo>>) => jsonResult.data)
          );
      } else {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', `${filter}`);
        return this.apiService.get('/project/jar/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<ProjectJarListVo>>) => jsonResult.data)
          );
      }

    } else {
      if (projectUuid) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('projectUuid', `${projectUuid}`);
        return this.apiService.get('/project/jar/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<ProjectJarListVo>>) => jsonResult.data)
          );
      } else {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`);
        return this.apiService.get('/project/jar/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<ProjectJarListVo>>) => jsonResult.data)
          );
      }
    }


  }

  /**
  * 获取License列表
  * @param pageIndex 页码
  * @param pageSize 每页几条信息
  */
  getLicensePage(pageIndex: number, pageSize: number, projectUuid?: string, filter?: string): Observable<PageVo<PorjectLicenseListsVo>> {
    if (filter && filter.length > 1 && filter.length < 50) {
      if (projectUuid) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', `${filter}`)
          .append('projectUuid', `${projectUuid}`);
        return this.apiService.get('/project/license/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<PorjectLicenseListsVo>>) => jsonResult.data)
          );
      } else {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', `${filter}`);
        return this.apiService.get('/project/license/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<PorjectLicenseListsVo>>) => jsonResult.data)
          );
      }
    } else {
      if (projectUuid) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('projectUuid', `${projectUuid}`);
        return this.apiService.get('/project/license/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<PorjectLicenseListsVo>>) => jsonResult.data)
          );
      } else {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`);
        return this.apiService.get('/project/license/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<PorjectLicenseListsVo>>) => jsonResult.data)
          );
      }
    }

  }

  /**
   * 获取 CVE 列表
   * @param pageIndex 页码
   * @param pageSize 每页几条信息
   */
  getCVEPage(pageIndex: number = 1, pageSize: number = 10, projectUuid?: string): Observable<PageVo<PorjectCVEListVo>> {
    if (projectUuid) {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`)
        .append('projectUuid', `${projectUuid}`);
      return this.apiService.get('/data/application/cve/list', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<PorjectCVEListVo>>) => jsonResult.data)
        );
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      return this.apiService.get('/data/application/cve/list', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<PorjectCVEListVo>>) => jsonResult.data)
        );
    }
  }
    // 看板CVE 增长趋势
  getCVEGrow() {
    return this.apiService.get('/app/count')
      .pipe(
        map((jsonResult: JsonResult<CVEGrowVo>) => jsonResult)
      );
  }
}